package edu.nus.iss.t5.jass.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.nus.iss.t5.jass.controller.JobResponseController;
import edu.nus.iss.t5.jass.entity.JobAdvertisement;
import java.util.List;
import javax.swing.JOptionPane;

/**
*  This is subclass of JPanel, which implements ActionListener interface
*  this panel is to show search results. 
*/
public class ResultPanel extends JPanel implements ActionListener {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JPanel tableJP;
    JPanel resultMainPanel;
    JPanel buttonsJP;
    JButton viewButton;
    JButton updateButton;
    JButton deleteButton;
    JButton listResponseButton;
    JPanel listResponseButtonJP;
    JPanel viewButtonJP;
    JPanel updateButtonJP;
    JPanel deleteButtonJP;
    JTable resultTable;
    JScrollPane scrollPane;
    ResultJobAdvertisementTable RJAT;
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    private List<JobAdvertisement> resultList;
    
    public ResultPanel() {
        //super(new GridLayout(1,0));
        tableJP = new JPanel(new BorderLayout());
        buttonsJP = new JPanel();
        createPanel();
        this.add(resultMainPanel);
    }
    
	/**
	 * Display Job Advertisements.  
	 * This method displays the received list of JobAdvertisement objects
	 * @param List of JobAdvertisement 
	 */
    public void displayAdList (List<JobAdvertisement> jl){
    	this.resultList = jl;
    	RJAT.updateData(jl);
    	RJAT.fireTableDataChanged();
   // 	System.out.println(resultTable.getRowCount());

    }
    private void createPanel() {
    	RJAT = new ResultJobAdvertisementTable();
        resultTable = new JTable(RJAT);
        resultTable.setPreferredScrollableViewportSize(new Dimension(xSize - 12, 400));
        scrollPane = new JScrollPane(resultTable);
        tableJP.add(scrollPane, BorderLayout.NORTH);

        Dimension d = new Dimension(50, 20);
        viewButton = new JButton("View Advertisement");
        viewButton.setSize(d);
        
        listResponseButton = new JButton("List All Responses");
        listResponseButton.setSize(d);
        listResponseButton.addActionListener(this);
        
        updateButton = new JButton("Update Advertisement");
        updateButton.setSize(d);
        
        deleteButton = new JButton("Delete Advertisement");
        deleteButton.setSize(d);
        
        buttonsJP = new JPanel(new GridLayout(1, 1));
        
        viewButtonJP = new JPanel();
        viewButton.setPreferredSize(new Dimension(160, 28));
        viewButton.addActionListener(this);
        viewButtonJP.add(viewButton);

        buttonsJP.add(viewButtonJP);

        updateButtonJP = new JPanel();
        updateButton.setPreferredSize(new Dimension(160, 28));
        updateButton.addActionListener(this);
        updateButtonJP.add(updateButton);
        buttonsJP.add(updateButtonJP);

        listResponseButtonJP = new JPanel ();
        listResponseButton.setPreferredSize(new Dimension(160, 28));
        listResponseButtonJP.add(listResponseButton);
        buttonsJP.add(listResponseButtonJP);
        
        deleteButtonJP = new JPanel();
        deleteButton.setPreferredSize(new Dimension(160, 28));
        deleteButton.addActionListener(this);
        deleteButtonJP.add(deleteButton);
        buttonsJP.add(deleteButtonJP);
        resultMainPanel = new JPanel(new BorderLayout());

        tableJP.add(buttonsJP, BorderLayout.SOUTH);
        //tableJP.setPreferredSize(new Dimension(xSize - 8, 520));
        //resultMainPanel.add(tableJP);
        resultMainPanel.add(tableJP, BorderLayout.NORTH);
        resultMainPanel.add(buttonsJP, BorderLayout.SOUTH);
    }
    
    /**
     * Implementation of ActionListener interface.
     * Captures all button actions, and invoke different panels for
     * different actions.
     */
    public void actionPerformed(ActionEvent event) {
        int i = -1;
        if ((i = resultTable.getSelectedRow()) < 0) {
            JOptionPane.showMessageDialog(this, "Please select a Job Advertisement!");
            return;
        } else {
            if (event.getSource() == viewButton) {

                //below pass selected item to view panel

                MainFrame mainFrame = new MainFrame();
                ViewPanel viewPanel = new ViewPanel(resultList.get(i));
                mainFrame.setviewPanel(viewPanel);
                Toolkit tk = Toolkit.getDefaultToolkit();
                int xSize = ((int) tk.getScreenSize().getWidth());
                int ySize = ((int) tk.getScreenSize().getHeight());
                mainFrame.setSize(xSize - 400, ySize - 300);
                mainFrame.setTitle("View JobAdvertisement");
                mainFrame.setVisible(true);


            }
            if (event.getSource() == updateButton) 
            {            	
            	if ((i = resultTable.getSelectedRow())>=0 && resultTable.getSelectedRowCount()==1)
            	{
    	        	MainFrame mainFrame = new MainFrame();
    	        	UpdatePanel updatePanel = new UpdatePanel(resultList.get(i));
    	        	mainFrame.setUpdatePanel(updatePanel);
    	        	Toolkit tk = Toolkit.getDefaultToolkit();
    	        	int xSize = ((int)tk.getScreenSize().getWidth());
    	        	int ySize = ((int)tk.getScreenSize().getHeight());
    	        	mainFrame.setSize(xSize-400, ySize-300);
    	        	mainFrame.setTitle("Update JobAdvertisement");
    	        	mainFrame.setVisible(true);
            	}
            	else if(resultTable.getSelectedRowCount()>1)
            	{
            		JOptionPane.showMessageDialog(this,"Please select only one JobAdvertisement to update.");
            	}
            	else if(resultTable.getSelectedRow()<1)
            	{
            		JOptionPane.showMessageDialog(this,"Please select one JobAdvertisement to update.");
            	}

            }
            if (event.getSource() == deleteButton) {

                MainFrame mainFrame = new MainFrame();
                DeletePanel deletePanel = new DeletePanel(resultList.get(i));
                mainFrame.setDeletePanel(deletePanel);
                Toolkit tk = Toolkit.getDefaultToolkit();
                int xSize = ((int) tk.getScreenSize().getWidth());
                int ySize = ((int) tk.getScreenSize().getHeight());
                mainFrame.setSize(xSize - 400, ySize - 300);
                mainFrame.setTitle("Delete JobAdvertisement");
                mainFrame.setVisible(true);

            }
            if (event.getSource() == listResponseButton) {

                JobResponseController.getInstance().showResponseListPanel(resultList.get(i));


            }

        }
    }
}
